package com.lzp.controller;

import com.lzp.pojo.bo.ShopcartBO;
import com.lzp.utils.JsonUtils;
import com.lzp.utils.RedisOperator;
import com.lzp.utils.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车API
 *
 * @author dtyy
 * @date 2020/8/18
 */
@Api(value = "购物车API", tags = "购物车相关API")
@RequestMapping("shopcart")
@RestController
public class ShopcartController {

    @Autowired
    private RedisOperator redis;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("/add")
    public ServerResponse add(@RequestParam String userId,
                              @RequestBody ShopcartBO shopcartBO,
                              HttpServletRequest request,
                              HttpServletResponse response) {

        if (StringUtils.isBlank(userId)) {
            return ServerResponse.createByErrorMsg("用户id不能为空");
        }

        // 用户客户端登录，添加商品到购物车，后端同步更新购物车到redis缓存
        // 如果当前购物车已经包含商品，则累加数量
        String redisKey = "foodie:shopcart:" + userId;
        String shopCartJson = redis.get(redisKey);
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNoneBlank(shopCartJson)) {
            // 此时Redis中存在购物车
            shopcartList = JsonUtils.jsonToList(shopCartJson, ShopcartBO.class);
            // 判断是否已经有商品，遍历更新值
            boolean isHaving = false;
            for (ShopcartBO sc : shopcartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(shopcartBO.getSpecId())) {
                    // 商品规格一致
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                // 商品不存在，则直接添加
                shopcartList.add(shopcartBO);
            }
        } else {
            // Redis中没有购物车
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }

        // 覆盖购物车
        // 此处的实现可以再细分，例如将商品放入单独的redis，商品数量等采用hset
        redis.set(redisKey, JsonUtils.objectToJson(shopcartList));

        return ServerResponse.createBySuccess();
    }

    @ApiOperation(value = "购物车删除商品", notes = "购物车删除商品", httpMethod = "POST")
    @PostMapping("/del")
    public ServerResponse del(@RequestParam String userId,
                              @RequestParam String itemSpecId,
                              HttpServletRequest request,
                              HttpServletResponse response) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return ServerResponse.createByErrorMsg("用户id或商品规格id不能为空");
        }

        // TODO同步删除后端购物车商品数据
        String redisKey = "foodie:shopcart:" + userId;
        String shopCartJson = redis.get(redisKey);
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNoneBlank(shopCartJson)) {
            // 此时Redis中存在购物车
            shopcartList = JsonUtils.jsonToList(shopCartJson, ShopcartBO.class);
            // 遍历找到要删除的商品
            for (ShopcartBO sc : shopcartList) {
                if (itemSpecId.equals(sc.getSpecId())) {
                    shopcartList.remove(sc);
                    break;
                }
            }
        }
        redis.set(redisKey, JsonUtils.objectToJson(shopcartList));
        return ServerResponse.createBySuccess();
    }

}
